//
//  NGHomeDetailViewController.swift
//  NingguoForum
//
//  Created by zhengsw on 2022/11/22.
//  Copyright © 2022 zhengsw. All rights reserved.
//

import UIKit
import WebKit

class NGHomeDetailViewController: NGBaseViewController ,WKScriptMessageHandler, WKNavigationDelegate{
    lazy var webView: WKWebView = {
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true

        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        configuration.userContentController = WKUserContentController()
//        configuration.userContentController.add(self, name: "AppModel")

        var webView = WKWebView(frame: CGRect(x: 0, y: KDevice_NaviBar_Height, width: KSCREEN_WIDTH, height: KSCREEN_HEIGHT - KDevice_NaviBar_Height), configuration: configuration)
        webView.scrollView.bounces = true
        webView.scrollView.alwaysBounceVertical = true
        webView.navigationDelegate = self
        return webView
    }()
    
    var link: String?
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        navigationBarView.backgroundColor = .white
        view.addSubview(webView)
        webView.load(URLRequest(url: URL(string:link ?? "")!))
        

    }
    func configUrl(link:String) {
        self.link = link
        if link.count > 0 {
            webView.load(URLRequest(url: URL(string:link)!))
        }
        
    }
 
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//            webView.evaluateJavaScript("sayHello('WebView你好！')") { [weak self] (result, err) in
//               print(result, err)
//           }
        }

        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            print(message.body)
        }
    deinit {
        print("NGHomeDetailViewController deinit")
    }
}
